package org.csu.logistics.Controller;

import org.csu.logistics.Service.MailService;
import org.csu.logistics.common.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;
import java.util.Random;

@Controller
@CrossOrigin(origins = "http://47.100.72.98:80")
@RequestMapping("/MailController/")
public class MailController {
    @Autowired
    private MailService mailService;
    private static int m;
    @RequestMapping("/CheckCode")
    @CrossOrigin(origins = "http://47.100.72.98:80")
    @ResponseBody
    public CommonResponse<String> getCheckCode(@RequestBody Map<String,String> email){
        String email_address = email.get("email");
        String checkCode = String.valueOf(new Random().nextInt(899999) + 100000);
        String message = "您的注册验证码为："+checkCode;
        m = Integer.parseInt(checkCode);
        try {
            mailService.sendSimpleMail(email_address, "注册验证码", message);
        }catch (Exception e){
            e.printStackTrace();
        }
        return CommonResponse.createForSuccess();
    }
    @RequestMapping("/JudgeCode")
    @CrossOrigin(origins = "http://47.100.72.98:80")
    @ResponseBody
    public CommonResponse<String> CheckResult(@RequestBody Map<String,String> checkcode){
        int code = Integer.parseInt(checkcode.get("checkcode"));
        if(code == m){
            return CommonResponse.createForSuccess();
        }
        return CommonResponse.createForError("验证码错误");
    }
}
